/*
 * Copyright (c) 2012 University of Tartu
 */
package org.qsardb.cargo.libsvm;

import java.io.*;

import libsvm.*;

import org.qsardb.model.*;

public class LibSVMCargo extends Cargo<Model> {

	public LibSVMCargo(Model model){
		super(ID, model);
	}

	@Override
	public boolean isBinary(){
		return false;
	}

	public svm_model loadModel() throws IOException {
		InputStream is = getInputStream();

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));

			try {
				return svm.svm_load_model(reader);
			} finally {
				reader.close();
			}
		} finally {
			is.close();
		}
	}

	public void storeModel(svm_model model) throws IOException {
		TempFilePayload payload = setTempFilePayload();

		File file = payload.getFile();

		svm.svm_save_model(file.getAbsolutePath(), model);
	}

	public static final String ID = "libsvm";
}